let zTree;

// zTree 配置
var zTreeSetting = {
    async: {
        enable: true,
        url: "/assignee/getSubNodesByDeptId",
        type: "get",
        autoParam: ["id=deptId"],
        otherParam: {"userName": ""}
    }, check: {
        enable: true,
        //chkStyle: "radio",
        radioType: "all"
    }
};


$(function () {

    //推送人
    $('#pushMsg input[name="toUserIds"]').click(function () {
        openModal();
    });

    //搜索
    $('.search button').click(function () {
        let search = $(this).parent().find('input').val();
        zTreeSetting.async.otherParam.userName = search;
        getToUserIds();
        //阻止默认事件
        return false;
    });

    //选择推送人
    $('#select').click(function () {
        var nodes = zTree.getCheckedNodes(true);
        var userIdArray = [];
        var userNameArray = [];
        $.each(nodes, function () {
            let node = this;
            if (node.isUser) {
                userIdArray.push(node.id);
                userNameArray.push(node.name);
            }
        });
        $('#pushMsg input[name="toUserIds"]').data('userIdArray', userIdArray);
        $('#pushMsg input[name="toUserIds"]').val(userNameArray.join(','));
        $('#toUserIds').modal('toggle');
    });

    //推送
    $('#pushMsg button').click(function () {
        let userIdArray = $('#pushMsg input[name="toUserIds"]').data('userIdArray');
        let title = $('#pushMsg input[name="title"]').val();
        let content = $('#pushMsg textarea[name="content"]').val();
        if ($.isEmptyObject(userIdArray)) {
            toastr.warning('请选择推送人');
            return false;
        }
        if ($.isEmptyObject(title)) {
            toastr.warning('请填写标题');
            return false;
        }
        if ($.isEmptyObject(content)) {
            toastr.warning('请填写内容');
            return false;
        }
        let toUserIds = userIdArray.join('|');
        let toUserNames = $('#pushMsg input[name="toUserIds"]').val();

        let workWxMessageEntity = {
            "toUserIds": toUserIds,
            "toUserNames": toUserNames,
            "title": title,
            "content": content
        };

        //console.log(workWxMessageEntity);

        $.ajax({
            type: "POST",
            url: "/pushMsg/push",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(workWxMessageEntity),
            success: function (data) {
                if (data.code === SUCCESS_CODE) {
                    toastr.success(data.msg);
                } else {
                    toastr.error(data.msg);
                }
            }
        });

        //阻止默认事件3
        return false;

    });
});

/**
 * 打开模态框，选择办理人
 */
function openModal() {
    $('#toUserIds').modal('toggle');
    getToUserIds();
}

function getToUserIds() {
    zTree = $.fn.zTree.init($("#toUserIdsTree"), zTreeSetting);
}